package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {


    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private StrategyTagMapper strategyTagMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

   // @Autowired
   // private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {

        //目的地处理:
        /**
         * 通过攻略id拿到整个大攻略
         * 大攻略有地点id,将大攻略里面的目的地对象放到攻略明细里
         *
         */
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();  //大攻略关联查询目的地
        detail.setDest(dest);

        //作者 此处忽略

        //摘要 summary
        //方案1: 由运营人员自己编写
        //方案2: 截取文章内容前200个字


        String content = detail.getStrategyContent().getContent();
        if(content.length() > 200){
            detail.setSummary(content.substring(0, 200));
        }else{
            detail.setSummary(content);
        }


        /**是否是国外
         *   通过吐司导航用目的地id获取吐司导航list,
         *   list的第一个就是顶级,就是国家
         */
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        Destination country = toasts.get(0);
        detail.setIsabroad(country.getId() != 1L);//true是国外,false是国内

        if(detail.getId() == null){
            //创建时间
            detail.setCreateTime(new Date());
            //插入明显
            strategyDetailMapper.insert(detail);

            //插入内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);


        }else{
            strategyDetailMapper.updateByPrimaryKey(detail);
            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
            //更新标签先打破关系再插入标签
            strategyDetailMapper.deleteRelation(detail.getId());
        }



        /**  插入标签    插入或者更新都用这个
         *      先判断用户是否有填写标签
         *      有的话解析出来插入
         *      维护中间表
         */
        if(StringUtils.hasLength(tags)){
            String[] ts = tags.split(",");
            for (String t : ts) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(t);
                strategyTagMapper.insert(strategyTag);
                //维护中间表   TODO 插入中间表有问题
                strategyDetailMapper.insertRelation(detail.getId(),strategyTag.getId());
            }
        }

    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long destId) {
        return strategyDetailMapper.selectViewNumTop3(destId);
    }

    @Override
    public void updateReplyNum(Long id, int replynum) {
        strategyDetailMapper.updateReplyNum(id, replynum);
    }

    @Override
    public List<StrategyDetail> queryViewNumTop3(Long id) {
        return strategyDetailMapper.selectViewNumTop3(id);
    }

    @Override
    public void StrategyPersistenceVoUpdate(StrategyPersistenceVo vo) {
        strategyDetailMapper.StrategyPersistenceVoUpdate(vo);
    }


    // @Override
    //public void updateStatisVO(StrategyStatisPersistenceVO vo) {
        //strategyDetailMapper.updateStatisVO(vo);
   // }
}
